package com.zimmur.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.solr.common.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zimmur.dao.designer.IDesigerInfoDao;
import com.zimmur.dao.goods.IGoodsDao;
import com.zimmur.dao.zone.ITopicDao;
import com.zimmur.service.ISolrService;
import com.zimmur.solr.model.Designer;
import com.zimmur.solr.model.Goods;
import com.zimmur.solr.model.Topic;
import com.zimmur.solr.service.ISolrDesignerService;
import com.zimmur.solr.service.ISolrGoodsService;
import com.zimmur.solr.service.ISolrTopicService;
@Service("solrService")
public class SolrServiceImpl implements ISolrService{
	Logger log=LoggerFactory.getLogger(SolrServiceImpl.class);
	@Resource
	private ISolrGoodsService solrGoodsService;
	@Resource
	private ISolrDesignerService solrDesignerService;
	@Resource
	private ISolrTopicService solrTopicService;
	@Resource
	private IGoodsDao goodsDao;
	@Resource
	private IDesigerInfoDao desigerInfoDao;
	@Resource
	private ITopicDao topicDao;
	@Override
	public int addGoods(String goodsId) {
		int flag=1;
		Goods goods=null;
		try {
			goods = goodsDao.selectGoodsByGoodsIdForSolr(Long.valueOf(goodsId));
			if (goods != null) {
				flag=solrGoodsService.add(goods);
			}
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
			flag=-1;
		}
		return flag;
	}

	@Override
	public int deleteGoods(String goodsIds) {
		int flag=0;
		try {
			String goodsId[]=goodsIds.split(",");
			for(String temp:goodsId){
				if(!StringUtils.isEmpty(temp))
					flag=solrGoodsService.deleteById(temp); 
			}
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
		}
		return flag;
	}

	@Override
	public int addDesigner(String userId) {
		int flag=1;
		try {
			Designer designer=desigerInfoDao.selectDesigerByUserIdForSolr(Long.valueOf(userId));
			if(designer!=null){
				flag=solrDesignerService.add(designer);
			}
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
			flag=-1;
		}
		return flag;
	}

	@Override
	public int deleteDesigner(String designers) {
		int flag=0;
		try {
			String designer[]=designers.split(",");
			for(String temp:designer){
				if(!StringUtils.isEmpty(temp))
					flag=solrDesignerService.delete(temp);
			}
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
			flag=-1;
		}
		return flag;
	}

	@Override
	public int addTopic(String topicId) {
		
		int flag=1;
		try {
			Topic topic=topicDao.selectByTopicIdForSolr(Long.valueOf(topicId));
			if(topic!=null){
				flag=solrTopicService.add(topic);
			}
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
			flag=-1;
		}
		return flag;
	}

	@Override
	public int deleteTopic(String topicIds) {
		int flag=0;
		try {
			String topicId[]=topicIds.split(",");
			for(String temp:topicId){
				if(!StringUtils.isEmpty(temp))
					flag=solrTopicService.deleteById(temp);
			}
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
			flag=-1;
		}
		return flag;
	}

	@Override
	public int addGoodses(String goodsIds) {
		int flag=1;
		try {
			List<Goods> goodses=goodsDao.selectGoodsByGoodsIdsForSolr(goodsIds);
			if(goodses.size()>0){
				flag=solrGoodsService.addList(goodses);
			}
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
			flag=-1;
		}
		return flag;	
	}

	@Override
	public int addDesigners(String userIds) {
		int flag=1;
		try {
			List<Designer> designers=desigerInfoDao.selectDesigerByUserIdsForSolr(userIds);
			if(designers.size()>0){
				flag=solrDesignerService.addList(designers);
			}
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
			flag=-1;
		}
		return flag;	
	}

	@Override
	public int addTopics(String topicIds) {
		int flag=1;
		try {
			List<Topic> topics=topicDao.selectByTopicIdsForSolr(topicIds);
			if(topics.size()>0){
				flag=solrTopicService.addlist(topics);
			}
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
			flag=-1;
		}
		return flag;	
	}

}
